import { ElMessage as ELMsg, ElMessageBox as MessageBox, ElLoading as Loading } from 'element-plus';

type MessageType =  "success" | "warning" | "info" | "error" | undefined;

interface IAlertOption {
    title?: string; // 提示标题
    showIcon?: boolean; // 是否显示图标
    type?: MessageType; // 消息类型，用于显示图标 success / info / warning / error
    confirmButtonText?: string; // 确定按钮的文本内容
}

interface IConfirmOption {
    title?: string; // 提示标题
    showIcon?: boolean; // 是否显示图标
    type?: MessageType; // 消息类型，用于显示图标 success / info / warning / error
    confirmButtonText?: string; // 确定按钮的文本内容
    cancelButtonText?: string; // 取消按钮的文本内容
}


interface IType {
    INFO: MessageType;
    SUCCESS: MessageType;
    ERROR: MessageType;
    WARNING: MessageType;
}

const MsgType: IType = {
    INFO: "info",
    SUCCESS: "success",
    ERROR: "error",
    WARNING: "warning",
};

class Message {

    public msgType: IType = MsgType;

    private loadingInstance: any = null;
    // 显示时间, 毫秒。设为 0 则不会自动关闭
    private duration: number = 3000;
    // 显示loading
    public loadingShow(text: string = '加载中', background: string = "rgba(255,255,255,0.7)") {
        this.loadingInstance = Loading.service({
            text,
            background
        });
    }
    // 隐藏loading
    public loadingHide() {
        if (this.loadingInstance != null) {
            this.loadingInstance.close();
            this.loadingInstance = null;
        }
    }

    // 普通的消息
    public info(msg: string = "") {
        if (msg === "") {
            return;
        }
        ELMsg({
            message: msg,
            type: this.msgType.INFO,
            duration: this.duration
        });
    }

    // 成功的消息
    public success(msg: string = "") {
        if (msg === "") {
            return;
        }
        ELMsg({
            message: msg,
            type: this.msgType.SUCCESS,
            duration: this.duration
        });
    }

    // 错误的消息
    public error(msg: string = "") {
        if (msg === "") {
            return;
        }
        ELMsg({
            message: msg,
            type: this.msgType.ERROR,
            duration: this.duration
        });
    }

    // 警告的消息
    public warning(msg: string = "") {
        if (msg === "") {
            return;
        }
        ELMsg({
            message: msg,
            type: this.msgType.WARNING,
            duration: this.duration
        });
    }

    // 消息提示
    public alert(msg: string = "", { title = '提示', showIcon = true, type = this.msgType.WARNING, confirmButtonText = '确定' }: IAlertOption = { title: '提示', showIcon: true, type: 'warning', confirmButtonText: '确定' }): Promise<any> {
        return MessageBox.alert(msg, title, {
            type: showIcon ? type : undefined,
            confirmButtonText
        });
    }

    // 确认提示  { title: '提示', type: 'warning', confirmButtonText: '确定', cancelButtonText: '取消' }
    public confirm(msg: string = "", { title = '提示', showIcon = true, type = this.msgType.WARNING, confirmButtonText = '确定', cancelButtonText = '取消' }: IConfirmOption = { title: '提示', showIcon: true, type: 'warning', confirmButtonText: '确定', cancelButtonText: '取消' }): Promise<any> {
        return MessageBox.confirm(msg, title, {
            type: showIcon ? type : undefined,
            confirmButtonText,
            cancelButtonText
        });
    }

}

const message: Message = new Message();
export default message;
